#!/bin/bash

# See 0.pv-ts.problem script and its description for problem statement.

# Solution 1 use bash streams (fd) redirections (by https://tldp.org/LDP/abs/html/x17974.html):
#  1) We copy descriptor 2 (stderr) into another copy (6)
#  2) Then use it in pv for out interractive progress.
# Behaviour (partial solution): Progress bar appeared as expected, but completely missing in the log!
# Sometimes it may be desired (e.g. to do not save big amount of logs), but in some cases we want store it in log also.
# For that see 2.pv-ts.dos2unix.log-fix semi-solution (and full 3.pv-ts.solution)

exec 6>&2 # Link file descriptor #6 with stderr (saves stderr) - we will use it in interractiv pv output.

exec &> >( ts '%d-%H:%M:%.S' | ts -i -- '+%H:%M:%.S' | tee -i -- "$(basename $0).$(date --iso-8601=s).log" )

echo Hi

dd if=/dev/urandom bs=1 count=100 | pv -s 100 --force -L 20 2>&6 > /dev/null

echo Bye
